/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zombiedefense.framework;

import java.awt.Dimension;
import java.awt.Graphics2D;
import zombiedefense.sprites.Player;
import zombiedefense.input.InputManager;
import zombiedefense.map.TileMap;
import zombiedefense.util.Vector2D;

/**
 *
 * @author &{user}
 */
public class PlayerManager extends GameObjectManager{

    private static Player player;
    
    public PlayerManager(Dimension gameDims, TileMap map,InputManager input,Player localPlayer) {
	super(gameDims, map, input);
	this.player = localPlayer;
    }
    
    public void update(long elapsedTime){
	//NOTE: these next three must be in a certain order.
	//updating moves the sprite.
	//CD moves the sprite to correct for collisions.
	//finally, rotating relies on the sprite's final postion.
	
        if(player.isExpired()){
            System.out.println("You died!");
            System.exit(0); //stop the program for now
        }
        
	player.update(elapsedTime);
	playerCD();
	playerRotate();
    }
    
    public void draw(Graphics2D g){
	super.drawObject(g,player);
    }
   
    public void playerCD(){
        Vector2D velocity = player.getVelocity();
        Vector2D location = player.getLocation();
        
	//player -> wall collision reaction
        //testing for horizontal movement
        if(map.isSolid(player.getX(),player.getY() + .5)){
            velocity.x = 0;
            location.x = Math.ceil(player.getX());
        }
	
	if(map.isSolid(player.getX() + 1,player.getY() + .5) && velocity.x > 0){
            velocity.x = 0;
            location.x = Math.floor(player.getX());
        }
        
        //testing for vertical movement
        if(map.isSolid(player.getX() + .5,player.getY())){
            velocity.y = 0;
            location.y = Math.ceil(player.getY());
        }
	
	if(map.isSolid(player.getX() + .5,player.getY() + 1)  && velocity.y > 0){
            velocity.y = 0;
            location.y = Math.floor(player.getY());
        }
    }
    
    public void playerRotate(){
	//rotate player towards mouse
        Vector2D.scratch.setTo(input.getMouseX(),input.getMouseY()); //mouse loc
        Vector2D.scratch2.setTo(map.getScreenX(player.centerX()),map.getScreenY(player.centerY())); //player center loc
        
        Vector2D.scratch.subtract(Vector2D.scratch2); //get vector between
        player.setRotation(Vector2D.scratch.getAngle());
	
	//rotate player's points
	player.rotatePoints();
    }
    
    public Player getPlayer(){
	return player;
    }
}
